/*
 * Copyright 2012 Marsrutai.lt
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lt.marsrutai.mobile.android.adapter;

import lt.marsrutai.mobile.android.R;
import lt.marsrutai.mobile.android.model.FastestRouteSearchBookmark;
import lt.marsrutai.mobile.android.util.StringUtils;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Adapter for displaying fastest route search bookmarks 
 * 
 * @author Justas
 */
public class FastestRouteSearchBookmarkAdapter extends BookmarkAdapter<FastestRouteSearchBookmark> {

    public FastestRouteSearchBookmarkAdapter(Context context) {
        super(context, R.layout.bookmark_fastest_route_search_item);
    }

    @Override
    public View doGetView(final int position, View view, final ViewGroup parent) {
        FastestRouteSearchBookmark bookmark = getItem(position);

        if (bookmark != null) {
            TextView name = (TextView) view.findViewById(R.id.name);
            name.setText(bookmark.getName());
            
            TextView fromTo = (TextView) view.findViewById(R.id.fromTo);
            String start = bookmark.getStart();
            String end = bookmark.getEnd();
            
            String fromToText = null;
            boolean startEmpty = StringUtils.isEmpty(start);
            boolean endEmpty = StringUtils.isEmpty(end);
            
            if (!startEmpty || !endEmpty) {
                if (!startEmpty && !endEmpty) {
                    fromToText = start + " - " + end;
                }
                else if (!startEmpty) {
                    fromToText = getContext().getString(R.string.bookmark_start, start);
                }
                else if (!endEmpty) {
                    fromToText = getContext().getString(R.string.bookmark_end, end);
                }
            }
            
            fromTo.setText(fromToText);
        }

        return super.doGetView(position, view, parent);
    }
}
